var i;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var EventManager = require("AA_EventManager");
var Module_WareHouse_UIBindings = require("AA_Module_WareHouse_UIBindings");
var AIControl = require("AA_AIControl");
var Const_Common = require("AA_Const_Common");
var GuideControl = require("AA_GuideControl");
var RedPointControl = require("AA_RedPointControl");
var EvenType = require("AA_EvenType");
var PlayerDataManager = require("AA_PlayerDataManager");
var Excel = require("AA_Excel");
var Logic_ChildMoveEvent = require("AA_Logic_ChildMoveEvent");
var Const_Hall = require("AA_Const_Hall");
var HallGlobalVariable = require("AA_HallGlobalVariable");
var Item_Bag_EquipItem = require("AA_Item_Bag_EquipItem");
var UI_Hall = require("AA_UI_Hall");
var BaseModule = require("AA_BaseModule");
var _decorator = cc._decorator;
var _ccclass = _decorator.ccclass;
var _property = _decorator.property;

var _def_Module_WareHouse = function (t) {
  function _creator() {
    var e = null !== t && t.apply(this, arguments) || this;
    e.auto_ui = null;
    e.equipItem = null;
    e.dragnode = null;
    return e;
  }
  __extends(_creator, t);
  _creator.prototype.onLoad = function () {
    EventManager.EventMgr.addEventListener(EvenType.EVENT_TYPE.HallBagShowDrag, this.ShowDrag, this);
    EventManager.EventMgr.addEventListener(EvenType.EVENT_TYPE.HallBagRemoveId, this.RemoveHero, this);
    EventManager.EventMgr.addEventListener(EvenType.EVENT_TYPE.HallResetBag, this.ResetThis, this);
    EventManager.EventMgr.addEventListener(EvenType.EVENT_TYPE.ResetWareHouseAI, this.EventResetAI, this);
  };
  _creator.prototype.onDestroy = function () {
    t.prototype.onDestroy.call(this);
    EventManager.EventMgr.rmEventListener(EvenType.EVENT_TYPE.HallBagShowDrag, this.ShowDrag, this);
    EventManager.EventMgr.rmEventListener(EvenType.EVENT_TYPE.HallBagRemoveId, this.RemoveHero, this);
    EventManager.EventMgr.rmEventListener(EvenType.EVENT_TYPE.HallResetBag, this.ResetThis, this);
    EventManager.EventMgr.rmEventListener(EvenType.EVENT_TYPE.ResetWareHouseAI, this.EventResetAI, this);
  };
  _creator.prototype.show = function () {
    var e = this;
    this.StartSet();
    this.scheduleOnce(function () {
      if (PlayerDataManager.PlayerDataMgr.GetGuideIndexByTaskName(Const_Common.GuideName.仓库升级) == Const_Common.UpEquipLevelGuideIndex.引导点击仓库水果2) {
        var t = e.auto_ui.node_dressupFather.children[0];
        if (t) {
          var o = t.convertToWorldSpaceAR(cc.v2(0, 0));
          var n = cc.size(t.width, t.height);
          GuideControl.GuideCtrl.ShowGuide({
            callBack: function () {
              PlayerDataManager.PlayerDataMgr.SetGuideIndex(Const_Common.GuideName.仓库升级, Const_Common.UpEquipLevelGuideIndex.引导点击升级3);
              t.getComponent(Item_Bag_EquipItem.default).OpenInfo();
            },
            wPos: o,
            size: n
          });
        }
      } else if (PlayerDataManager.PlayerDataMgr.GetGuideIndexByTaskName(Const_Common.GuideName.水果上阵) == Const_Common.DressUpEquipGuideIndex.选择水果) {
        e.scheduleOnce(function () {
          var t = e.auto_ui.node_unLockFather.children[0];
          if (t) {
            var o = t.convertToWorldSpaceAR(cc.v2(0, 0));
            var n = cc.size(t.width, t.height);
            GuideControl.GuideCtrl.ShowGuide({
              callBack: function () {
                PlayerDataManager.PlayerDataMgr.SetGuideIndex(Const_Common.GuideName.水果上阵, Const_Common.DressUpEquipGuideIndex.引导穿戴);
                t.getComponent(Item_Bag_EquipItem.default).OpenInfo();
              },
              wPos: o,
              size: n
            });
          }
        }, .15);
      } else {
        e.AICheck();
      }
    });
  };
  _creator.prototype.hide = function () {};
  _creator.prototype.onEnable = function () {
    HallGlobalVariable.default.isNeedResetWareHouse && this.upLevelMap && this.ResetThis();
  };
  _creator.prototype.ResetThis = function () {
    HallGlobalVariable.default.isNeedResetWareHouse = false;
    this.ShowDragView(false);
    this.ResetDressUp();
    this.ResetUnLock();
    this.AICheck();
    this.ResetLock();
  };
  _creator.prototype.StartSet = function () {
    var e = this;
    if (!this.upLevelMap) {
      this.shuiGuoArray = [];
      Excel.Excel.shuiguo_all.forEach(function (t) {
        -1 != t.isShow && e.shuiGuoArray.push(t);
      });
      this.upLevelMap = new Map();
      Excel.Excel.shuiguo_all.forEach(function (t) {
        -1 != t.isShow && e.upLevelMap.set(t.id, []);
      });
      Excel.Excel.shuiguolevel_all.forEach(function (t) {
        e.upLevelMap.get(t.shuiguoId).push(t);
      });
    }
    this.ShowDragView(false);
    this.ResetDressUp();
    this.ResetUnLock();
    this.ResetLock();
  };
  _creator.prototype.ResetDressUp = function () {
    this.auto_ui.node_dressupFather.children.forEach(function (e) {
      e.active = false;
    });
    var e = 0;
    var t = PlayerDataManager.PlayerDataMgr.dressUpArray;
    var o = Excel.Excel.wareHouseControl(1).unLockArray;
    for (var n = 0; n < t.length; n++) {
      var i = t[n];
      var a = undefined;
      if (n < this.auto_ui.node_dressupFather.childrenCount) {
        a = this.auto_ui.node_dressupFather.children[n];
      } else {
        (a = cc.instantiate(this.equipItem)).parent = this.auto_ui.node_dressupFather;
      }
      a.active = true;
      var r = a.getComponent(Item_Bag_EquipItem.default);
      var c = new Const_Hall.BagEquipShowData();
      c.shuiGuoId = i;
      -1 == i && (c.unLockChar = o[n]);
      if (i > 0) {
        e++;
        c.upLevelDatas = this.upLevelMap.get(i);
      }
      r.StartSet(c);
    }
    this.auto_ui.txt_allCountLabel.string = e + "/" + t.length;
  };
  _creator.prototype.ResetUnLock = function () {
    this.auto_ui.node_unLockFather.children.forEach(function (e) {
      e.active = false;
    });
    this.shuiGuoArray.sort(function (e, t) {
      return t.quality - e.quality;
    });
    var e = PlayerDataManager.PlayerDataMgr.dressUpArray;
    var t = 0;
    for (var o = 0; o < this.shuiGuoArray.length; o++) {
      var n = this.shuiGuoArray[o].id;
      if (PlayerDataManager.PlayerDataMgr.GetEquipDataById(n).isUnLock && -1 == e.indexOf(n)) {
        var i = undefined;
        if (t < this.auto_ui.node_unLockFather.childrenCount) {
          i = this.auto_ui.node_unLockFather.children[t];
        } else {
          (i = cc.instantiate(this.equipItem)).parent = this.auto_ui.node_unLockFather;
        }
        i.active = true;
        var a = i.getComponent(Item_Bag_EquipItem.default);
        var r = new Const_Hall.BagEquipShowData();
        r.shuiGuoId = n;
        r.upLevelDatas = this.upLevelMap.get(n);
        a.StartSet(r);
        t++;
      }
    }
    this.auto_ui.node_unLockFather.active = t > 0;
    this.auto_ui.node_unLockFather.parent.getChildByName("已解锁").active = t > 0;
  };
  _creator.prototype.ResetLock = function () {
    this.auto_ui.node_lockFather.children.forEach(function (e) {
      e.active = false;
    });
    var e = 0;
    var t = [];
    for (var o = 0; o < this.shuiGuoArray.length; o++) {
      var n = (i = this.shuiGuoArray[o]).id;
      PlayerDataManager.PlayerDataMgr.GetEquipDataById(n).isUnLock || t.push(i);
    }
    t.sort(function (e, t) {
      return e.unLock - t.unLock;
    });
    for (o = 0; o < t.length; o++) {
      var i = t[o];
      var a = undefined;
      if (e < this.auto_ui.node_lockFather.childrenCount) {
        a = this.auto_ui.node_lockFather.children[e];
      } else {
        (a = cc.instantiate(this.equipItem)).parent = this.auto_ui.node_lockFather;
      }
      a.active = true;
      var r = a.getComponent(Item_Bag_EquipItem.default);
      var c = new Const_Hall.BagEquipShowData();
      c.shuiGuoId = i.id;
      c.upLevelDatas = this.upLevelMap.get(i.id);
      r.StartSet(c);
      e++;
    }
    this.auto_ui.node_lockFather.active = e > 0;
    this.auto_ui.node_lockFather.parent.getChildByName("未解锁").active = e > 0;
  };
  _creator.prototype.ShowDrag = function (e) {
    this.ShowDragView(true);
    if (!this.dragnode) {
      this.dragnode = cc.instantiate(this.equipItem);
      this.dragnode.parent = this.auto_ui.node_dragFather;
      this.dragnode.setPosition(0, 0);
      this.auto_ui.node_dragFather.addComponent(Logic_ChildMoveEvent.default).StartSet(this.onChildTouchStart.bind(this), this.onChildTouchMove.bind(this), this.onChildTouchUp.bind(this));
      this.auto_ui.node_clickCardNode.addComponent(Logic_ChildMoveEvent.default).StartSet(this.onChildTouchStart.bind(this), this.onChildTouchMove.bind(this), this.onChildTouchUp.bind(this));
      this.dragStartPos = this.auto_ui.node_dragFather.getPosition();
    }
    this.dragnode.active = true;
    var t = new Const_Hall.BagEquipShowData();
    t.shuiGuoId = e;
    t.upLevelDatas = this.upLevelMap.get(e);
    this.dragnode.getComponent(Item_Bag_EquipItem.default).StartSet(t, false);
    this.dragShuiGuoId = e;
    AIControl.default._instance.isOpen && this.ChangeOne();
  };
  _creator.prototype.onChildTouchStart = function (e) {
    e.target == this.auto_ui.node_dragFather && this.auto_ui.node_dragFather.setPosition(this.auto_ui.node_dragFather.parent.convertToNodeSpaceAR(e.getLocation()));
  };
  _creator.prototype.onChildTouchMove = function (e) {
    e.target == this.auto_ui.node_dragFather && this.auto_ui.node_dragFather.setPosition(this.auto_ui.node_dragFather.parent.convertToNodeSpaceAR(e.getLocation()));
  };
  _creator.prototype.onChildTouchUp = function (e) {
    var t = this.auto_ui.node_dressupFather.convertToNodeSpaceAR(e.getLocation());
    for (var o = 0; o < this.auto_ui.node_dressupFather.children.length; o++) {
      var n = this.auto_ui.node_dressupFather.children[o];
      if (cc.Vec2.distance(t, n.getPosition()) < 100) {
        var i = n.getComponent(Item_Bag_EquipItem.default);
        if (-1 == PlayerDataManager.PlayerDataMgr.globalData.dressUpArray[o]) {
          break;
        }
        i.openData.shuiGuoId > 0 && PlayerDataManager.PlayerDataMgr.RemoveDressUpEquip(i.openData.shuiGuoId, o);
        PlayerDataManager.PlayerDataMgr.AddDressUpEquip(this.dragShuiGuoId, o);
        EventManager.EventMgr.dispatchEvent(EvenType.EVENT_TYPE.MainRedPointChange, RedPointControl.RedPointType.upEquip);
        this.ResetDressUp();
        this.ResetUnLock();
        this.ShowDragView(false);
        break;
      }
    }
    e.target == this.auto_ui.node_dragFather && this.auto_ui.node_dragFather.setPosition(this.dragStartPos);
  };
  _creator.prototype.RemoveHero = function (e) {
    e > 0 && PlayerDataManager.PlayerDataMgr.RemoveDressUpEquip(e);
    this.ResetDressUp();
    this.ResetUnLock();
  };
  _creator.prototype.ShowDragView = function (e) {
    this.auto_ui.node_scrollView.active = !e;
    this.auto_ui.node_dragView.active = e;
    this.auto_ui.node_clickCardNode.active = e;
    this.ShowScaleTween(e);
  };
  _creator.prototype.ShowScaleTween = function (e) {
    this.auto_ui.node_dressupFather.children.forEach(function (t) {
      t.stopAllActions();
      t.scale = 1;
      e && t.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(.8, 1.1, 1.1), cc.scaleTo(.8, .9, .9))));
    });
  };
  _creator.prototype.EventResetAI = function () {
    var e = this;
    EventManager.EventMgr.dispatchEvent(EvenType.EVENT_TYPE.MainRedPointChange, RedPointControl.RedPointType.upEquip);
    this.scheduleOnce(function () {
      e.AICheck();
    }, AIControl.default._instance.aiCDTime);
  };
  _creator.prototype.AICheck = function () {
    var e = this;
    AIControl.default._instance.isOpen && this.scheduleOnce(function () {
      if (e.CheckIsNeedDressUp()) {
        e.DressUpOne();
      } else {
        e.UpOneLevel() || AIControl.default._instance.ShowClickBtn(UI_Hall.default._instance.auto_ui.btn_fightingBtn.node, function () {
          UI_Hall.default._instance.ShowViewByType(2);
        });
      }
    });
  };
  _creator.prototype.CheckIsNeedDressUp = function () {
    var e = PlayerDataManager.PlayerDataMgr.dressUpArray.filter(function (e) {
      return 0 == e;
    });
    if (e && e.length > 0) {
      this.aiCurrUnLockArray = [];
      var t = 0;
      var o = null;
      for (var n = 0; n < this.auto_ui.node_unLockFather.children.length; n++) {
        var i = this.auto_ui.node_unLockFather.children[n];
        var a = Excel.Excel.shuiguo(i.getComponent(Item_Bag_EquipItem.default).openData.shuiGuoId);
        if (a.quality > t) {
          o = a;
          t = a.quality;
        }
      }
      this.aiCurrUnLockArray = [o];
      return true;
    }
    this.aiCurrDressUpArray = [];
    this.aiCurrUnLockArray = [];
    this.shuiGuoArray.sort(function (e, t) {
      return t.quality - e.quality;
    });
    var r = PlayerDataManager.PlayerDataMgr.dressUpArray;
    for (n = 0; n < this.shuiGuoArray.length; n++) {
      var c = (i = this.shuiGuoArray[n]).id;
      if (PlayerDataManager.PlayerDataMgr.GetEquipDataById(c).isUnLock) {
        if (-1 == r.indexOf(c)) {
          this.aiCurrUnLockArray.push(i);
        } else {
          this.aiCurrDressUpArray.push(i);
        }
      }
    }
    return this.aiCurrDressUpArray[this.aiCurrDressUpArray.length - 1].quality < this.aiCurrUnLockArray[0].quality;
  };
  _creator.prototype.DressUpOne = function () {
    var t = function (e) {
      var t = o.auto_ui.node_unLockFather.children[e];
      if (t.getComponent(Item_Bag_EquipItem.default).openData.shuiGuoId == o.aiCurrUnLockArray[0].id) {
        AIControl.default._instance.ShowClickBtn(t, function () {
          t.getComponent(Item_Bag_EquipItem.default).OpenInfo(true);
        });
        return {
          value: undefined
        };
      }
    };
    var o = this;
    for (var n = 0; n < this.auto_ui.node_unLockFather.children.length; n++) {
      var i = t(n);
      if ("object" == typeof i) {
        return i.value;
      }
    }
  };
  _creator.prototype.ChangeOne = function () {
    var t = this;
    var o = function (e) {
      var o = n.auto_ui.node_dressupFather.children[e];
      if (o.getComponent(Item_Bag_EquipItem.default).openData.shuiGuoId == n.aiCurrDressUpArray[n.aiCurrDressUpArray.length - 1].id) {
        var i = o.getComponent(Item_Bag_EquipItem.default);
        if (-1 == PlayerDataManager.PlayerDataMgr.globalData.dressUpArray[e]) {
          return "break";
        } else {
          AIControl.default._instance.ShowClickBtn(o, function () {
            i.openData.shuiGuoId > 0 && PlayerDataManager.PlayerDataMgr.RemoveDressUpEquip(i.openData.shuiGuoId, e);
            PlayerDataManager.PlayerDataMgr.AddDressUpEquip(t.dragShuiGuoId, e);
            t.ResetDressUp();
            t.ResetUnLock();
            t.ShowDragView(false);
            t.AICheck();
          });
          return {
            value: undefined
          };
        }
      }
    };
    var n = this;
    for (var i = 0; i < this.auto_ui.node_dressupFather.children.length; i++) {
      var a = o(i);
      if ("object" == typeof a) {
        return a.value;
      }
      if ("break" === a) {
        break;
      }
    }
  };
  _creator.prototype.UpOneLevel = function () {
    function _creator(e) {
      AIControl.default._instance.ShowClickBtn(e.node, function () {
        e.OpenInfo();
      });
    }
    for (var t = 0; t < this.auto_ui.node_dressupFather.children.length; t++) {
      if ((o = this.auto_ui.node_dressupFather.children[t].getComponent(Item_Bag_EquipItem.default)).GetCanUp()) {
        _creator(o);
        return true;
      }
    }
    for (t = 0; t < this.auto_ui.node_unLockFather.children.length; t++) {
      var o;
      if ((o = this.auto_ui.node_unLockFather.children[t].getComponent(Item_Bag_EquipItem.default)).GetCanUp()) {
        _creator(o);
        return true;
      }
    }
    return false;
  };
  __decorate([_property(Module_WareHouse_UIBindings.default)], _creator.prototype, "auto_ui", undefined);
  __decorate([_property(cc.Prefab)], _creator.prototype, "equipItem", undefined);
  return __decorate([_ccclass], _creator);
}(BaseModule.default);
exports.default = _def_Module_WareHouse;